<template>
  <div class="login-page">
    <div class="back">
      <!-- .native绑定组件的原生事件，属于组件根元素 -->
      <!-- $router.back() 返回上一次访问路由，forward go -->
      <van-icon @click.native="$router.back()" name="arrow-left"></van-icon>
    </div>
    <h3 class="title">短信登录</h3>
    <!-- 表单 -->
    <van-form class="form" ref="form" @submit="login">
      <van-field
        placeholder="请输入手机号"
        v-model="form.mobile"
        :rules="rules.mobile"
        format-trigger="onBlur"
      ></van-field>
      <van-field
        placeholder="请输入验证码"
        v-model="form.code"
        :rules="rules.code"
      >
        <template #button>
          <span class="send" @click="getMobileCode" v-if="count == 60"
            >发送验证码</span
          >
          <span class="send" v-else>{{ count }}秒</span>
        </template></van-field
      >
      <van-button block round type="info" native-type="submit">登录</van-button>
    </van-form>
  </div>
</template>
<script>
import { getMobileCodeAPI, loginAPI } from "@/api/userApi";
import { Notify } from "vant";
import { mapActions, mapMutations } from "vuex";

export default {
  name: "LoginPage",
  data() {
    return {
      form: {
        mobile: "",
        code: "",
      },
      interValObj: null,
      count: 60,
      rules: {
        mobile: [
          { required: true, message: "请输入手机号" },
          { pattern: /^1[3-9]\d{9}$/, message: "手机号格式错误" },
        ],
        code: [
          { required: true, message: "请输入手机号" },
          { pattern: /^\d{6}$/, message: "验证码为6位纯数字" },
        ],
      },
    };
  },
  methods: {
    ...mapMutations("user", ["updateTokenInfo", "updateUserInfo"]),
    ...mapMutations("chat", ["initWebSocket"]),
    ...mapActions("user", ["initProfile"]),
    async getMobileCode() {
      const { data: res } = await getMobileCodeAPI(this.form.mobile);
      if (res.message === "OK") {
        setInterval(() => {
          if (this.count === 0) {
            clearInterval();
            this.count = 60;
          } else {
            this.count--;
          }
        }, 1000);
      } else {
        Notify({
          type: "warning",
          message: res.message,
          duration: 2000,
        });
      }
    },
    async login() {
      const { data: res } = await loginAPI(this.form.mobile, this.form.code);
      if (res.message === "OK") {
        this.updateTokenInfo(res.tokenInfo);
        this.initProfile();
        this.initWebSocket(res.tokenInfo.token);
        const navPath = this.$route.query.pre || "/";
        this.$router.replace(navPath);
      } else {
        Notify({
          type: "warning",
          message: res.message,
          duration: 2000,
        });
      }
    },
    SetRemainTime() {
      window.clearInterval(this.interValObj);
    },
  },
};
</script>
<style scoped lang="less">
.login-page {
  padding: 0 32px;
  .back {
    height: 60px;
    display: flex;
    align-items: center;
    .van-icon {
      font-size: 25px;
      color: #ccc;
      position: relative;
      left: -15px;
    }
  }
  .title {
    font-size: 22px;
    line-height: 1;
    padding: 30px 0;
  }
  .van-cell {
    padding: 20px 0;
    &::after {
      left: 0;
      right: 0;
    }
  }
  .van-button {
    width: 100%;
    margin-top: 40px;
    height: 50px;
    color: #fff;
    font-size: 16px;
    border: none;
    background: linear-gradient(to right, #ff9999, #ffa179);
  }
}

.send {
  font-size: 12px;
  color: #a5a6ab;
}
</style>
